//
//  ZDStoreDetialController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/15.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDStoreDetialController.h"
#import "ZDStoreModel.h"
#import "ZDGoodsDetailModel.h"
#import "ZDStoreHeader.h"
#import "ZDGoodsListCell.h"

#import "ZDGoodsDetialController.h"
#import "ZDMapController.h"

static NSString *const ZDStoreHeaderId = @"ZDStoreHeader";
static NSString *const ZDGoodsListCellId = @"ZDGoodsListCell";

@interface ZDStoreDetialController ()

/**
 <#Description#>
 */
@property(nonatomic,strong)ZDStoreModel *storeModel;

@end

@implementation ZDStoreDetialController

- (void)setupUI
{
    [super setupUI];
    self.title = self.storeName;
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZDStoreHeader" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ZDStoreHeaderId];
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZDGoodsListCell" bundle:nil] forCellWithReuseIdentifier:ZDGoodsListCellId];
    
}
- (void)loadData
{
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.storeId forKey:@"store_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;

    [API netAccess:ServiceStoreDetialUrl andMethor:@"GET" andDict:dict success:^(NSDictionary *successCode)
    {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        NSString *error = datas[@"error"];
        if (error)
        {
            [weakSelf addNoDataViewWithTipText:error];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [weakSelf hiddenNoDataView];
            
            NSArray *tempt = datas[@"goods_list"];
            NSArray *tempModel = [ZDGoodsDetailModel mj_objectArrayWithKeyValuesArray:tempt];

            [weakSelf.dataSource removeAllObjects];
            [weakSelf.dataSource addObjectsFromArray:tempModel];
            
            weakSelf.storeModel = [ZDStoreModel mj_objectWithKeyValues:datas[@"store_info"]];
            NSString *servieNumber =[NSString stringWithFormat:@"服务次数: %d",[weakSelf getSum:weakSelf.dataSource]] ;
            weakSelf.storeModel.serviceNumber = servieNumber;
            
        }
        [weakSelf.collectionView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}
#pragma mark- collectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return self.dataSource.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(LEScreenWidth, 300);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((LEScreenWidth -24)/2, 200);
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 8, 0, 8);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 8;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
    if (kind == UICollectionElementKindSectionHeader)
    {

        ZDStoreHeader *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:ZDStoreHeaderId forIndexPath:indexPath];
        headerView.storeModel = self.storeModel;
        [headerView.addressButton addTarget:self action:@selector(showInMap) forControlEvents:UIControlEventTouchUpInside];
        return headerView;

    }
    return nil;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    ZDGoodsListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ZDGoodsListCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return  cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (![[NSUserDefaults standardUserDefaults] boolForKey:ISLOGIN])
    {
        [self presentLoginController];
        return ;
    }
    ZDGoodsDetailModel *goods = self.dataSource[indexPath.row];
    ZDGoodsDetialController *goodsDetial = [[ZDGoodsDetialController alloc] init];
    goodsDetial.goodsId = goods.goods_id;
    goodsDetial.goodsName = goods.goods_name;
    [self.navigationController pushViewController:goodsDetial animated:YES];
}
#pragma mark- 点击事件

/**
 跳转到地图上
 */
- (void)showInMap
{
    ZDMapController *mapController = [[ZDMapController alloc] init];
    mapController.storeModel = self.storeModel;
    [self.navigationController pushViewController:mapController animated:YES];
}
#pragma mark- privateMethod

/**
 求商品购买次数的和

 @param array <#array description#>
 @return <#return value description#>
 */
- (int)getSum:(NSArray *)array
{
    int sum = 0;
    for (int i = 0; i < array.count; i++) {
        ZDGoodsDetailModel *model = array[i];
        sum += [model.goods_salenum intValue];
    }
    return sum;
}

@end
